package learn_concurrent._4;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class T6 {
	static final Logger logger = LoggerFactory.getLogger(T6.class);

	public static void main(String[] args) {
		
		GuardedObject guardedObject = new GuardedObject();
		
		new Thread(() -> {
			logger.info("get value....");
			Object value = guardedObject.get();
			logger.info("value = {}", value);
		}, "t1").start();
		
		new Thread(() -> {
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			logger.info("complete value...");
			guardedObject.complete(Integer.valueOf(1001));
		}, "t2").start();
		
	}

}

class GuardedObject {

	private Object response;

	public Object get() {
		synchronized (this) {
			while (response == null) {
				try {
					this.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			return response;
		}
	}

	public void complete(Object value) {
		synchronized (this) {
			response = value;
			this.notifyAll();
		}
	}
}
